/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_RTMP_SESSION_H
#define MK_RTMP_SESSION_H
#include "MkTcp/MkTcpSession.h"
#include "MkMedia/MkMediaSource.h"
#include "MkRtmpUtil.h"
#include "MkRtmpParam.h"

class CMkRtmpPublisherSource;
class CMkRtmpConsumer;
class MkApi CMkRtmpSession : public CMkTcpSession, public CMkRtmpParam
{
    typedef enum _rtmp_publisher_state
    {
        MkPublishSuccess,
        MkPublishScript,
        MkPublishVideo
    }MkRtmpPublisherState;
public:
    CMkRtmpSession(CMkEvent* pEvent, CMkTimer* pTimer, CMkTcpServer* pServer,const MkMediaCallbacks& MediaCallbacks);
    ~CMkRtmpSession();

    Uint32 SendFlvStream(const MkEsPacket& EsPacket, const CMkBufferList& BufferList);
    void SetNonePublishSource() { m_pRtmpPublishSource = nullptr; }
protected:
private:
    Uint32 OnRecv() override;
    Uint32 OnEventRemoved() override;
    Uint32 OnCommandRequest(const MkString& Command, const MkVector<MkAmfObject>& VecObj) override;
    Uint32 OnVideo(const Uint8* pBuf, Uint32 nLen, Uint32 TimeStamp) override;
    Uint32 OnAudio(const Uint8* pBuf, Uint32 nLen, Uint32 TimeStamp) override;
    Uint32 OnSetDataFrame(const MkVector<MkAmfObject>& VecObj);
private:
    Uint32 ParseHandSharkeC0(const Uint8* pBuf, Uint32 nLen, Uint32& nUsed);
    Uint32 ParseHandSharkeC1(const Uint8* pBuf, Uint32 nLen, Uint32& nUsed);
    Uint32 ParseHandSharkeC2(const Uint8* pBuf, Uint32 nLen, Uint32& nUsed);
    Uint32 ParsePublish(const Uint32& TransactionId);
    Uint32 ParsePlay(const Uint32& TransactionId);
    Uint32 ParsePause(const Uint32& TransactionId, BOOL bPause, Uint32 MilliSec);
    Uint32 ParseSeek(const Uint32& TransactionId, Uint32 MilliSec);
    Uint32 SendHandSharkeS0S1S2();
    Uint32 SendChunkSize(Uint32 ChunkSize);
    Uint32 SendAcknowledgement();
    Uint32 SendWindownAcknowledgementSize();
    Uint32 SendSetPeerBandWidth(Uint32 BandWidth);
    Uint32 SendStreamBegin();
    Uint32 SendOnBWDone();
    Uint32 SendConnectResult(const Uint32& TransactionId, const MkVector<MkAmfObject>& VecObj);
    Uint32 SendCreateStreamResult(const Uint32& TransactionId);
    Uint32 SendPlayResult(const Uint32& TransactionId, BOOL bSuccess);
    Uint32 SendPauseResult(const Uint32& TransactionId, BOOL bPause, BOOL bSuccess);
    Uint32 SendSeekResult(const Uint32& TransactionId, BOOL bSuccess);
    Uint32 SendPublishResult(const Uint32& TransactionId, BOOL bSuccess);
private:
    MkMediaCallbacks                m_MediaCbs;
    MkRtmpState                     m_RtmpState;
    MkString                        m_LocalSig;
    MkString                        m_RemoteSig;
    MkString                        m_AppName;
    MkString                        m_StreamPath;
    CMkRtmpPublisherSource*         m_pRtmpPublishSource;
    CMkRtmpConsumer*                m_pRtmpConsumer;
    MkList<MkCacheStreamPacket>     m_ListCacheStream;
};


#endif


